<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/wye3.jpg" alt="" />
        <span>原神后台管理系统</span>
      </div>
      <el-button type="warn" @click="logout">退出</el-button>
    </el-header>

    <el-container class="AsideMain">
      <!-- 测栏 -->
      <el-aside :width="isShow ? '64px' : '200px'">
        <div class="show-aside" @click="asideShow">|||</div>
        <el-menu
          class="el-menu-vertical-demo"
          active-text-color="rgb(162, 97, 76)"
          unique-opened
          :collapse="isShow"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- item是第一层数据 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 一级导航 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级导航 -->
            <!-- 怎样引用2级列表里面的children的数据 ->上面定义过来item 这里用subItem代替item-->
            <el-menu-item
              :index="'/' + subItem.path + ''"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavStatus('/' + subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 子组件 -->
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      menulist: [], //因为res.data是数据形式，所以写成:[]接收data
      iconsObj: {
        125: "iconfont icon-yonghutianchong",
        103: "iconfont icon-quanxianguanli1",
        101: "iconfont icon-gouwuchefill",
        102: "iconfont icon-dingdanguanli",
        145: "iconfont icon-shujutongji",
      },
      isShow: false,
      activePath: "", //高亮路径
    };
  },

  created() {
    //先填充数据再渲染页面
    this.getMenuList();
    //为高亮设置的 (刷新仍高亮)
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    async getMenuList() {
      //获取后端menus数据
      const { data: res } = await this.$http.get("menus"); //axios代替this.$http
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menulist = res.data; //将数据放在menulist中 上面的data()中
      console.log(res);
    },
    //测栏伸缩
    asideShow() {
      this.isShow = !this.isShow;
    },
    saveNavStatus(activePath) {
      ////为高亮设置的 (刷新仍高亮)
      window.sessionStorage.setItem("activeStatus", activePath);
      this.activePath = activePath;
    },
  },
};
</script>

<style lang="less" scoped>
.el-header {
  background-color: rgb(148, 90, 63);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 20px;
  color: white;
  > div {
    display: flex;
    align-items: center;
  }
  span {
    margin-left: 15px;
  }
}
.el-header img {
  height: 61px;
  border-radius: 50%;
}

.el-aside {
  background-color: rgba(161, 94, 73, 0.986);
}
.el-menu {
  border-right: none;
}

.el-main {
  background-color: #fff5f5c9;
}
.home-container {
  height: 100%;
}
.iconfont {
  margin-right: 5px;
}
.show-aside {
  height: 30px;
  width: 100%;
  background-color: rgb(150, 66, 66);
  text-align: center;
  color: white;
  line-height: 30px;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>